package com.aj.activity.adapter;

import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.aj.activity.R;
import com.aj.databases.entry.AlbumPhotoInfo;
import com.aj.databases.entry.BabyPhotoInfo;
import com.aj.utils.PortraitLoaderUtil;

public class ChildPhotoAdapter extends BaseAdapter {

	private Context context;

	private List<BabyPhotoInfo> babyPhotoInfoArrayList;

	private int imageMaxWidth = 0;
	private int imageMaxHeight = 0;

	private HashMap<Integer, View> viewCache = new HashMap<Integer, View>();

	public ChildPhotoAdapter(Context context,
			List<BabyPhotoInfo> babyPhotoInfoArrayList) {
		this.context = context;
		this.babyPhotoInfoArrayList = babyPhotoInfoArrayList;
		// int[] widthAndHeight = ScreenUtils.getScreenDispaly(context);
		imageMaxWidth = (int) context.getResources().getDimension(
				R.dimen.dimen_120);
		imageMaxHeight = (int) context.getResources().getDimension(
				R.dimen.dimen_70);
		// if (widthAndHeight != null && widthAndHeight.length > 1) {
		// float widthscale = (widthAndHeight[0] / 480f);
		// imageMaxWidth *= widthscale;
		// imageMaxHeight *= widthscale;
		// }
	}

	@Override
	public int getCount() {
		return babyPhotoInfoArrayList.size();
	}

	@Override
	public Object getItem(int position) {
		return babyPhotoInfoArrayList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	class ViewHolder {
		Button age_namber;
		ImageView photo_1;
		ImageView photo_2;
		ImageView photo_3;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		convertView = viewCache.get(position);
		BabyPhotoInfo babyPhotoInfo = babyPhotoInfoArrayList.get(position);
		ViewHolder viewHodler = null;
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(
					R.layout.item_child_photo, null);
			viewHodler = new ViewHolder();
			viewHodler.age_namber = (Button) convertView
					.findViewById(R.id.age_namber);
			viewHodler.photo_1 = (ImageView) convertView
					.findViewById(R.id.photo_1);
			viewHodler.photo_2 = (ImageView) convertView
					.findViewById(R.id.photo_2);
			viewHodler.photo_3 = (ImageView) convertView
					.findViewById(R.id.photo_3);
			viewHodler.photo_1.setLayoutParams(new LinearLayout.LayoutParams(
					imageMaxWidth, imageMaxHeight));
			viewHodler.photo_2.setLayoutParams(new LinearLayout.LayoutParams(
					imageMaxWidth, imageMaxHeight));
			viewHodler.photo_3.setLayoutParams(new LinearLayout.LayoutParams(
					imageMaxWidth, imageMaxHeight));
			convertView.setTag(viewHodler);
			viewCache.put(position, convertView);
		} else {
			viewHodler = (ViewHolder) convertView.getTag();
		}
		viewHodler.age_namber.setText(babyPhotoInfo.getPhotoAge());
		List<AlbumPhotoInfo> albumPhotoInfoList = babyPhotoInfo
				.getAlbumPhotoInfoList();
		if (albumPhotoInfoList == null || albumPhotoInfoList.size() == 0) {
			viewHodler.photo_1.setVisibility(View.INVISIBLE);
			viewHodler.photo_2.setVisibility(View.INVISIBLE);
			viewHodler.photo_3.setVisibility(View.INVISIBLE);
		} else if (albumPhotoInfoList.size() == 1) {
			viewHodler.photo_1.setVisibility(View.VISIBLE);
			PortraitLoaderUtil.loadSrc(context, viewHodler.photo_1, null,
					albumPhotoInfoList.get(0).getAlbumUrl(),
					R.drawable.child_photo_nomorl,
					R.drawable.child_photo_nomorl);
			viewHodler.photo_2.setVisibility(View.INVISIBLE);
			viewHodler.photo_3.setVisibility(View.INVISIBLE);
		} else if (albumPhotoInfoList.size() == 2) {
			viewHodler.photo_1.setVisibility(View.VISIBLE);
			PortraitLoaderUtil.loadSrc(context, viewHodler.photo_1, null,
					albumPhotoInfoList.get(0).getAlbumUrl(),
					R.drawable.child_photo_nomorl,
					R.drawable.child_photo_nomorl);
			viewHodler.photo_2.setVisibility(View.VISIBLE);
			PortraitLoaderUtil.loadSrc(context, viewHodler.photo_2, null,
					albumPhotoInfoList.get(1).getAlbumUrl(),
					R.drawable.child_photo_nomorl,
					R.drawable.child_photo_nomorl);
			viewHodler.photo_3.setVisibility(View.INVISIBLE);
		} else if (albumPhotoInfoList.size() >= 3) {
			viewHodler.photo_1.setVisibility(View.VISIBLE);
			PortraitLoaderUtil.loadSrc(context, viewHodler.photo_1, null,
					albumPhotoInfoList.get(0).getAlbumUrl(),
					R.drawable.child_photo_nomorl,
					R.drawable.child_photo_nomorl);
			viewHodler.photo_2.setVisibility(View.VISIBLE);
			PortraitLoaderUtil.loadSrc(context, viewHodler.photo_2, null,
					albumPhotoInfoList.get(1).getAlbumUrl(),
					R.drawable.child_photo_nomorl,
					R.drawable.child_photo_nomorl);
			viewHodler.photo_3.setVisibility(View.VISIBLE);
			PortraitLoaderUtil.loadSrc(context, viewHodler.photo_3, null,
					albumPhotoInfoList.get(2).getAlbumUrl(),
					R.drawable.child_photo_nomorl,
					R.drawable.child_photo_nomorl);
		}
		return convertView;
	}

	public int getImageMaxWidth() {
		return imageMaxWidth;
	}

	public void setImageMaxWidth(int imageMaxWidth) {
		this.imageMaxWidth = imageMaxWidth;
	}

	public int getImageMaxHeight() {
		return imageMaxHeight;
	}

	public void setImageMaxHeight(int imageMaxHeight) {
		this.imageMaxHeight = imageMaxHeight;
	}

}
